package com.jdb.banner.controller;

import com.guns21.jackjson.annotation.JsonMixin;
import com.guns21.jackjson.annotation.JsonResponse;
import com.guns21.jackjson.mixins.ResultMixin;
import com.guns21.result.domain.PageResult;
import com.guns21.result.domain.Result;
import com.guns21.support.controller.BaseController;
import com.guns21.web.constant.SpringConstant;
import com.jdb.banner.api.dto.BannerDTO;
import com.jdb.banner.api.query.BannerQuery;
import com.jdb.banner.controller.assembler.BannerAssembler;
import com.jdb.banner.controller.mixins.BannerDTOMixin;
import com.jdb.banner.provider.entity.BannerDO;
import com.jdb.banner.provider.service.BannerCommandService;
import com.jdb.banner.provider.service.BannerQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chhw on 2016/11/7.
 */
@RestController
@RequestMapping("/api/banner/v1")
public class BannerController extends BaseController {

    @Autowired
    private BannerQueryService bannerQueryService;
    @Autowired
    private BannerCommandService bannerCommandService;

    /**
     * banner列表
     *
     * @param count
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @JsonResponse(mixins = {@JsonMixin(target = PageResult.class, mixin = ResultMixin.class),
            @JsonMixin(target = BannerDTO.class, mixin = BannerDTOMixin.class)})
    public PageResult bannerList(@RequestParam(defaultValue = "5", required = false) Integer count) throws Exception {
        return bannerQueryService.listBanners(count);
    }

    // --------------------------------------------------------------
    @PostMapping("/save")
    public Result<BannerDTO> save(@RequestBody BannerDTO loanDTO) {
        BannerDO loanDO = BannerAssembler.fromDTO(loanDTO);
        return bannerCommandService.save(loanDO);
    }

    @GetMapping("/listByPage")
    public PageResult listByPage(BannerQuery loanQuery) {

        return bannerQueryService.listByPage(loanQuery);
    }

    @GetMapping(value = "/detailAll/{id:" + SpringConstant.UUID_REGEX_32 + "}")
    public Result detailAll(@PathVariable String id) {
        return bannerQueryService.get(id);
    }
}
